/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kn.mx.cstf2.actions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Map;
import kn.cstf2.assign.Bean.T_usersBean;
import kn.cstf2.assign.Shared.AssignShared;
import kn.cstf2.global.Utils.CstF2Exception;
import kn.cstf2.users.Bean.UserBean;
import kn.cstf2.users.Bean.UsersBean;
import kn.cstf2.users.Server.UsersServer;
import kn.cstf2.users.Shared.UsersShared;

/**
 *
 * @author user1
 */
public class UsersAction extends ActionSupport {

  
    public static final String ERROR_SESSION = "errorSession";
    public static final String ERROR_KNKEY = "errorKnkey";
    public static final String PRINCIPALUSUARIO = "asign";
    public static final String RESULTUSUARIO = "result";
    private int direccionAction;
    private String userName;
    private String eMail;
    private UsersBean userData;
    private UsersBean userDataModificar;
    private Integer idUserSelected;
    ArrayList<UsersBean> users;
    private ArrayList<StringBuilder> usersSpring;
    private ArrayList<String> tipos;
    private String username;
    private String pwd;
    private String email;
    private Boolean SUA;
    private String Imp;
    private String Tran;
    private String Cop;

    @Override
    public String execute() {
        switch (direccionAction) {
            case 1:
                return PRINCIPALUSUARIO;
        }
        return PRINCIPALUSUARIO;
    }

    public String GetUsersWithSameNameAndEmail() {
        try {
            UsersShared usersshared = new UsersShared();
            String usertype = "";
            int contador = 0;
            ArrayList<String> users = new ArrayList<String>();

            users = usersshared.GetUsersWithSameNameAndEmail(getUserData().getUsername(), getUserData().getEmail());

            if (users.isEmpty()) {
                UsersBean userBean = new UsersBean();

                userBean.setUsername(getUserData().getUsername());
                userBean.setPwd(getUserData().getPwd());
                userBean.setEmail(getUserData().getEmail());

                userBean.setCcr(getUserData().getCcr());
                userBean.setCcrro(getUserData().getCcrro());
                userBean.setMng(getUserData().getMng());
                userBean.setMngro(getUserData().getMngro());
                userBean.setOpc(getUserData().getOpc());
                userBean.setOpcro(getUserData().getOpcro());
                userBean.setRco(getUserData().getRco());
                userBean.setRcoro(getUserData().getRcoro());
                userBean.setRg(getUserData().getRg());
                userBean.setRgro(getUserData().getRgro());
                userBean.setSu(getUserData().getSu());

                if (getUserData().getCcr()) {
                    if (contador == 0) {
                        usertype += "CCR";
                        contador++;
                    } else {
                        usertype += " - " + "CCR";
                    }
                }
                if (getUserData().getCcrro()) {
                    if (contador == 0) {
                        usertype += "CCR/RO";
                        contador++;
                    } else {
                        usertype += " - " + "CCR/RO";
                    }
                }
                if (getUserData().getMng()) {
                    if (contador == 0) {
                        usertype += "MNG";
                        contador++;
                    } else {
                        usertype += " - " + "MNG";
                    }
                }
                if (getUserData().getMngro()) {
                    if (contador == 0) {
                        usertype += "MNG/RO";
                        contador++;
                    } else {
                        usertype += " - " + "MNG/RO";
                    }
                }
                if (getUserData().getOpc()) {
                    if (contador == 0) {
                        usertype += "OPC";
                        contador++;
                    } else {
                        usertype += " - " + "OPC";
                    }
                }
                if (getUserData().getOpcro()) {
                    if (contador == 0) {
                        usertype += "OPC/RO";
                        contador++;
                    } else {
                        usertype += " - " + "OPC/RO";
                    }
                }
                if (getUserData().getRco()) {
                    if (contador == 0) {
                        usertype += "RCO";
                        contador++;
                    } else {
                        usertype += " - " + "RCO";
                    }
                }
                if (getUserData().getRcoro()) {
                    if (contador == 0) {
                        usertype += "RCO/RO";
                        contador++;
                    } else {
                        usertype += " - " + "RCO/RO";
                    }
                }
                if (getUserData().getRg()) {
                    if (contador == 0) {
                        usertype += "RG";
                        contador++;
                    } else {
                        usertype += " - " + "RG";
                    }
                }
                if (getUserData().getRgro()) {
                    if (contador == 0) {
                        usertype += "RG/RO";
                        contador++;
                    } else {
                        usertype += " - " + "RG/RO";
                    }
                }
                if (getUserData().getSu()) {
                    if (contador == 0) {
                        usertype += "SU";
                        contador++;
                    } else {
                        usertype += " - " + "SU";
                    }
                }


                userBean.setUsertype(usertype);
                usersshared.SaveNewUser(userBean);
            }

        } catch (CstF2Exception ex) {
            
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }

        return PRINCIPALUSUARIO;
    }

    public String GetUsersByName() {
        try {

            UsersShared usersshared = new UsersShared();
            users = usersshared.GetUsersByName(username);

            usersSpring = new ArrayList<StringBuilder>();
            usersSpring = usersshared.GetModificarUsersByName(username);

        } catch (CstF2Exception ex) {
            
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return PRINCIPALUSUARIO;
        }

        return RESULTUSUARIO;
    }

    public String GetUserInfo() {
        try {
            UsersShared usersshared = new UsersShared();
            AssignShared assignShared = new AssignShared();
            T_usersBean t_usersBean = new T_usersBean();

            t_usersBean = assignShared.GetDataUsersFromId(idUserSelected);


        } catch (CstF2Exception ex) {
            
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        return SUCCESS;
    }

    public String UpdateUserData() {
        try {
            
            UserBean userBean = new UserBean();
            UsersServer usersServer = new UsersServer();

            userBean.setEmail(email);
            userBean.setPwd(pwd);
            userBean.setUsername(username);
            userBean.setSu(SUA);
            userBean.setId(userDataModificar.getId().intValue());

            if(Tran.equals("NA")){
                userBean.setTransitionAccess(false);
                userBean.setTransition(false);
            }
            if(Tran.equals("RO")){
                userBean.setTransitionAccess(true);
                userBean.setTransition(false);
            }
            if(Tran.equals("E")){
                userBean.setTransitionAccess(true);
                userBean.setTransition(true);
            }
            if(Imp.equals("NA")){
                userBean.setImplementationAccess(false);
                userBean.setImplementation(false);
            }
            if(Imp.equals("RO")){
                userBean.setImplementationAccess(true);
                userBean.setImplementation(false);
            }
            if(Imp.equals("E")){
                userBean.setImplementationAccess(true);
                userBean.setImplementation(true);
            }
            if(Cop.equals("NA")){
                userBean.setCopAccess(false);
                userBean.setCop(false);
            }
            if(Cop.equals("RO")){
                userBean.setCopAccess(true);
                userBean.setCop(false);
            }
            if(Cop.equals("E")){
                userBean.setCopAccess(true);
                userBean.setCop(true);
            }

            if(SUA){
                userBean.setTransitionAccess(true);
                userBean.setTransition(true);
                userBean.setImplementationAccess(true);
                userBean.setImplementation(true);
                userBean.setCopAccess(true);
                userBean.setCop(true);
            }

            usersServer.modifyUser(userBean);

        } catch (CstF2Exception ex) {
            
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());

        }
        return RESULTUSUARIO;
    }

    public String DeleteUserClient() {
        try {
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesionName = contexto.getSession();
            userData = new UsersBean();
            getUserData().setUsername((String) sesionName.get("nameSearch"));

            UsersShared usersshared = new UsersShared();

            usersshared.DeleteUserClientDependences(getIdUserSelected());
            usersshared.DeleteUserClient(getIdUserSelected());

            users = usersshared.GetUsersByName(getUserData().getUsername());

        } catch (CstF2Exception ex) {
            
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());

        }
        return RESULTUSUARIO;
    }

    public String SaveNewUser() {
        UserBean userBean = new UserBean();
        UsersServer usersServer = new UsersServer();
        
        userBean.setEmail(email);
        userBean.setPwd(pwd);
        userBean.setUsername(username);
        userBean.setSu(SUA);
        
        if(Tran.equals("NA")){
            userBean.setTransitionAccess(false);
            userBean.setTransition(false);
        }
        if(Tran.equals("RO")){
            userBean.setTransitionAccess(true);
            userBean.setTransition(false);
        }
        if(Tran.equals("E")){
            userBean.setTransitionAccess(true);
            userBean.setTransition(true);
        }
        if(Imp.equals("NA")){
            userBean.setImplementationAccess(false);
            userBean.setImplementation(false);
        }
        if(Imp.equals("RO")){
            userBean.setImplementationAccess(true);
            userBean.setImplementation(false);
        }
        if(Imp.equals("E")){
            userBean.setImplementationAccess(true);
            userBean.setImplementation(true);
        }
        if(Cop.equals("NA")){
            userBean.setCopAccess(false);
            userBean.setCop(false);
        }
        if(Cop.equals("RO")){
            userBean.setCopAccess(true);
            userBean.setCop(false);
        }
        if(Cop.equals("E")){
            userBean.setCopAccess(true);
            userBean.setCop(true);
        }
        
        if(SUA){
            userBean.setTransitionAccess(true);
            userBean.setTransition(true);
            userBean.setImplementationAccess(true);
            userBean.setImplementation(true);
            userBean.setCopAccess(true);
            userBean.setCop(true);
        }
            
        usersServer.SaveNewUser(userBean);
        
        email = "";
        username = "";
        pwd = "";
                
        
        return "SUCCESS";
    }

    public static void main(String args[]) {
    }
    
    public UsersBean getUserDataModificar() {
        return userDataModificar;
    }

    public void setUserDataModificar(UsersBean userDataModificar) {
        this.userDataModificar = userDataModificar;
    }

    public ArrayList<StringBuilder> getUsersSpring() {
        return usersSpring;
    }

    public void setUsersSpring(ArrayList<StringBuilder> usersSpring) {
        this.usersSpring = usersSpring;
    }

    public ArrayList<UsersBean> getUsers() {
        return users;
    }

    public void setUsers(ArrayList<UsersBean> users) {
        this.users = users;
    }

    public int getDireccionAction() {
        return direccionAction;
    }

    public void setDireccionAction(int direccionAction) {
        this.direccionAction = direccionAction;
    }

    public Integer getIdUserSelected() {
        return idUserSelected;
    }

    public void setIdUserSelected(Integer idUserSelected) {
        this.idUserSelected = idUserSelected;
    }

    public String geteMail() {
        return eMail;
    }

    public void seteMail(String eMail) {
        this.eMail = eMail;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public UsersBean getUserData() {
        return userData;
    }

    public void setUserData(UsersBean userData) {
        this.userData = userData;
    }

    public ArrayList<String> getTipos() {
        return tipos;
    }

    public void setTipos(ArrayList<String> tipos) {
        this.tipos = tipos;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Boolean getSUA() {
        return SUA;
    }

    public void setSUA(Boolean SUA) {
        this.SUA = SUA;
    }

    public String getImp() {
        return Imp;
    }

    public void setImp(String Imp) {
        this.Imp = Imp;
    }

    public String getTran() {
        return Tran;
    }

    public void setTran(String Tran) {
        this.Tran = Tran;
    }

    public String getCop() {
        return Cop;
    }

    public void setCop(String Cop) {
        this.Cop = Cop;
    }
}